// 用于配置对应路由
let express = require('express');
let userquery = require('../api/user_query');
const jwt = require('jsonwebtoken');
const { createToken } = require("../api/token");
var urlLib = require("url");
let router = express.Router();
router.use(express.json());
router.use(express.urlencoded({
    extended: true
}));

//查询所有管理员账号
router.get('/alllogins', (req, res) => {
    userquery.getUser(function (rec) {
        res.json(rec).end();
    });
});

//用户登录
router.get('/login', (req, res) => {
    var query = urlLib.parse(req.url, true).query;
    userquery.getLoginAccount(query, function (req2) {
        if (req2.length === 0 || req2[0].username === undefined) {
            res.send({
                success: false,
                message: 'error'
            });
        } else {
            const { username } = req2[0].username;
            const Token = createToken({ username });
            res.send({
                success: true,
                message: 'success',
                Token
            });
        }
        console.log(req2);
    });
});

//获取指定的作者信息 {通过id}
router.get('/login/:auother_id', function (req, res) {
    var auother_id = parseInt(req.params.auother_id);
    userquery.get(auother_id, function (rec) {
        res.json(rec).end();
    });
    console.log(auother_id);
});

//获取指定name的作者信息 {通过username}
router.get('/userinfo/:username', function (req, res) {
    console.log(req,"req");
    var username = req.params.username;
    userquery.getUsername(username, function (rec) {
        res.json(rec).end();
    });
    console.log(username,"username");
});

//使用POST方法添加用户信息
router.post('/register', function (req, res) {
    var reqbody = req.body;
    userquery.getUser(function (rec) {
        let existName = []
        rec.forEach(element => {
            existName.push(element.username);
            console.log(element);
        });
        console.log(existName.includes(reqbody.username));
        if (existName.includes(reqbody.username)) {
            res.send({
                success: false,
                message: '用户名已经存在',
            });
            return null;
        } else {
            var arr = [];
            //遍历JSON数据中所有的值
            for (var key in reqbody) {
                arr.push(reqbody[key]);
            }
            userquery.add([
                reqbody.username,
                reqbody.gender,
                reqbody.phone,
                reqbody.email,
                reqbody.state,
                reqbody.role_id,
                reqbody.password
            ], function (rec) {
                res.json(rec).end();
            });
        }
    });
});
router.get('/user_role', (req, res) => {
    userquery.getUserRole(function (rec) {
        res.json(rec).end();
    });
});
//使用DELETE方法删除用户信息
router.delete('/cancle/:id', function (req, res) {
    var auother_id = parseInt(req.params.id);
    // var reqbody = req.body;
    //先判断ID是否有效，再执行删除操作
    userquery.get(auother_id, function (rec) {
        if (rec.length == 0) {
            res.status(404).json({
                msg: '指定ID的作者不存在！'
            });
        } else {
            userquery.del(auother_id, function (p) {
                res.json(rec).end();
            });
        }
    });
});
module.exports = router